import { userApi } from '/api/user'
import { getUser } from '/store/actions'
import { isLogin } from '/store/mutations'
Page({
  data: {
    columns: [
      {
        key: 'title',
        title: '比赛名称',
        width: 360,
      },
      {
        key: 'address',
        title: '比赛地点',
        width: 240,
      },
      {
        key: 'match_date',
        title: '比赛日期',
        width: 260,
      },
      {
        key: 'first_name',
        width: 200,
        title: '冠军',
      },
      {
        key: 'first_bonus',
        width: 200,
        title: '冠军奖金',
      },
      {
        key: 'second_name',
        width: 240,
        title: '亚军',
      },
      {
        key: 'second_bonus',
        width: 240,
        title: '亚军奖金',
      },
      {
        key: 'score',
        width: 200,
        title: '比赛比分',
      },
    ],
    tableData: [
      // {
      //   title: '第六届集梦单挑赛',
      //   score: '3:0',
      //   address: '河北石家庄',
      //   first_name: '赵俊毅',
      //   second_name: '王晨鑫',
      //   second_bonus: 0,
      //   first_bonus: 5000,
      // },
      // {
      //   title: '第五届集梦单挑赛',
      //   score: '3:1',
      //   address: '湖南醴陵',
      //   first_name: '陈子安',
      //   second_name: 'Slim',
      //   second_bonus: 1000,
      //   first_bonus: 4000,
      // },
      // {
      //   title: '第四届集梦单挑赛',
      //   score: '3:1',
      //   address: '浙江舟山',
      //   first_name: '陈子安',
      //   second_name: 'Slim',
      //   second_bonus: 1000,
      //   first_bonus: 4000,
      // },
      // {
      //   title: '第三届集梦单挑赛',
      //   score: '3:2',
      //   address: '河南开封',
      //   first_name: '孙彬凯',
      //   second_name: '李波',
      //   second_bonus: 2000,
      //   first_bonus: 3000,
      // },
      // {
      //   title: '第二届集梦单挑赛',
      //   score: '3:0',
      //   address: '河北廊坊',
      //   first_name: '刘健',
      //   second_name: '杰瑞',
      //   second_bonus: 0,
      //   first_bonus: 2000,
      // },
      // {
      //   title: '第一届集梦单挑赛',
      //   score: '3:0',
      //   first_name: '杰瑞',
      //   second_name: '陈子安',
      //   quantity: '1000',
      //   address: '辽宁朝阳',
      //   second_bonus: 0,
      //   first_bonus: 5000,
      // },
    ],
    navData: [
      { name: '冠军榜', id: 1, page: 30, isLastpage: false },
      //{ name: '亚军榜', id: 2, page: 30, isLastpage: false },
      { name: '奖金榜', id: 3, page: 30, isLastpage: false },
      { name: '积分榜', id: 4, page: 30, isLastpage: false },
      { name: '比赛记录', id: 5, page: 30, isLastpage: false },
    ],
    cur: 1,
    startX: 0,
    list: [] as any[],
    follower: false,
    scrollClass: false,
    user: {},
  },
  onLoad() {
    // if (!isLogin()) {
    //   checkAuth()
    // }
    // $event.on('after:login', this, () => {
    //   this.getUserList()
    // })
    this.getUserList()
    console.log(this.data.navData)
  },
  async onPullDownRefresh() {
    await this.getUserList()
    wx.stopPullDownRefresh()
  },
  toChangeCurrent(e: WechatMiniprogram.BaseEvent) {
    const id = e.currentTarget.dataset.id as number
    if (id === this.data.cur) return
    this.setData({
      cur: id,
    })
    console.log(id)
    if (id <= 4) {
      this.getUserList()
    } else {
      this.getMatchRecordList()
    }
  },
  async getMatchRecordList(isLoadMore = false) {
    //const index = this.data.cur
    const offset = 0 // isLoadMore ? this.data.list[index].length + 10 : 0
    const list = await userApi.getPersonMacthRecordList({ offset })
    // this.setData({
    //   [`list[${index}]`]:
    //     isLoadMore && this.data.list[index] ? [...this.data.list[index], ...list] : list,
    // })
    this.setData({
      tableData: list,
    })
  },
  onReachBottom() {
    if (this.data.navData[this.data.cur].isLastpage) {
      wx.showToast({
        title: '已没有更多',
        icon: 'none',
        duration: 1000,
      })
    } else {
      this.getUserList(true)
    }
  },
  onPageScroll() {
    this.fixedTitle()
  },
  /** 判断是否滚动到了导航 */
  fixedTitle() {
    const query = wx.createSelectorQuery().in(this)
    query.select('#header').boundingClientRect()
    query.selectViewport().scrollOffset()
    query.exec((res: any) => {
      this.setData({
        scrollClass: res[0] && res[0].bottom < 0,
      })
    })
  },
  chooseTab(e: WechatMiniprogram.BaseEvent) {
    const index = e.currentTarget.dataset.index
    if (index === this.data.cur) return
    this.setData({
      cur: index,
    })
    if (this.data.list[this.data.cur]) return
    this.getUserList()
  },
  async getUserList(isLoadMore = false) {
    if (isLoadMore) {
      wx.showLoading({
        title: '加载中',
      })
    }
    const index = this.data.cur
    console.log(index)
    const offset = isLoadMore ? this.data.list[index].length : 0
    const list =
      index === 1
        ? await userApi.getChampionUser({ offset })
        : index === 2
        ? await userApi.getSecondUser({ offset })
        : index === 3
        ? await userApi.getBonusUser({ offset })
        : await userApi.getScoreUser({ offset })
    this.setData({
      [`list[${index}]`]:
        isLoadMore && this.data.list[index] ? [...this.data.list[index], ...list] : list,
    })
    if (isLogin()) {
      this.setData({
        user: getUser(),
      })
    }
    wx.hideLoading()
  },
  async follower(e: WechatMiniprogram.BaseEvent) {
    if (this.data.follower) return
    this.setData({
      follower: true,
    })
    const index = e.currentTarget.dataset.index
    const cur = this.data.cur
    const list = this.data.list[cur]
    list[index].has_follower
      ? await userApi.unFollowUser(list[index].id)
      : await userApi.followUser(list[index].id)
    this.setData({
      [`list[${cur}][${index}].has_follower`]: !list[index].has_follower,
      follower: false,
    })
    wx.showToast({
      title: list[index].has_follower ? '关注成功' : '已取消关注',
      icon: 'none',
      duration: 1000,
    })
  },
})
